// miniprogram/pages/prize/prize.js
const app = getApp();

Page({ 

  /**
   * 页面的初始数据
   */
  data: {
    prizeNum:0,
    hasPrize:"none",
    noPrize:"block",
    prizeLevel:"",
    prizeGoods:"",
    isShareScope:"none",
    code:"",
    isShow:true
  },
  //定义分享小程序的界面
  onShareAppMessage: function(){
    return {
      path: "/pages/index/index"
    }
  },

  showPrize(){ //渲染奖品栏
    if(this.data.prizeNum != 0){
      this.setData({
        hasPrize:"block",
        noPrize:"none"
      })
    }
  },
  getPrize(){ //渲染奖品内容
    var obj = app.globalData.prize[this.data.prizeNum-1];
    var levelText;
    if(this.data.prizeNum == 1){
      levelText = "一等奖";
    }else if(this.data.prizeNum == 2){
      levelText = "二等奖";
    }else if(this.data.prizeNum == 3){
      levelText = "三等奖";
    }
    this.setData({
      prizeLevel:levelText,
      prizeGoods:obj.goods
    })
  },
  infoWrite(){ //跳转信息填写页面
    wx.navigateTo({
      url:"../write/write?prizeNum="+this.data.prizeNum
    })
  },
  shareScope(){ //点击按钮显示出小程序二维码
    this.setData({
      isShareScope:"block"
    })
  },
  saveImage (filePath) {
    var that = this;
    wx.downloadFile({ //下载图片
      url: filePath,
      success: function (res) {
        that.setData({
          code: res.tempFilePath  
        })
        wx.saveImageToPhotosAlbum({
          filePath: that.data.code,  //图片路径
          success: (res) => {
            console.log(res)
            wx.showToast({
              title:"保存成功",
              icon:"success",
              duration:2000
            });
            that.setData({
              isShareScope:"none"
            })
          },
          fail: (err) => {
            console.log(err)
            wx.showToast({
              title:"保存失败，请稍后重试",
              icon:"none",
              duration:2000
            });
          }
        })
      }
    })
  },
  // 点击保存图片到相册(授权)
  saveImageToPhotos (filePath) {
    let that = this
    // 相册授权
    wx.getSetting({
      success(res) {
        // 进行授权检测，未授权则进行弹层授权
        if (!res.authSetting['scope.writePhotosAlbum']) {
          wx.authorize({
            scope: 'scope.writePhotosAlbum',
            success () {
              that.saveImage(filePath)
            },
            // 拒绝授权时，则进入手机设置页面，可进行授权设置
            fail() {
              wx.openSetting({
                success: function (data) {
                  console.log("成功");
                },
                fail: function (data) {
                  console.log("失败");
                }
              });
            }
          })
        } else {
          // 已授权则直接进行保存图片
          that.saveImage(filePath)
        }
      },
      fail(res) {
        console.log(res);
      }
    })
  },
  // 点击保存按钮触发
  clickSave () {
    this.setData({
      isShow:false
    })
    this.saveImageToPhotos("https://7465-test-dez9p-1259161577.tcb.qcloud.la/images/erweima.jpg?sign=380db82e49add34f71c0abdb94bfa924&t=1557405590");
  },
  //长按识别二维码
  previewImage(){
    this.setData({
      isShow:false
    })
    wx.previewImage({
      current: "https://7465-test-dez9p-1259161577.tcb.qcloud.la/images/erweima.jpg?sign=380db82e49add34f71c0abdb94bfa924&t=1557405590",
      urls: ["https://7465-test-dez9p-1259161577.tcb.qcloud.la/images/erweima.jpg?sign=380db82e49add34f71c0abdb94bfa924&t=1557405590"]
    })
  },
  hide(){
    if(this.data.isShow){
      this.setData({
        isShareScope:"none"
      })
    }
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var prizeNum = decodeURIComponent(options.prizeNum);
    this.setData({
      prizeNum:prizeNum
    })
    console.log(this.data.prizeNum)
    if(prizeNum != 0){
      this.showPrize();
      this.getPrize();
    }
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})